package com.tnsw.facebookautoliker;

import java.sql.Date;
import java.util.Calendar;

import com.tnsw.coreutils.AsyncManager;
import com.tnsw.coreutils.MyLog;
import com.tnsw.coreutils.files.FileUtils;
import com.tnsw.coreutils.text.TextUtils;
import com.tnsw.facebookautoliker.services.AutoLikerService;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Environment;
import android.util.Log;

public class MainReceiver extends BroadcastReceiver {

	private String tag = "MainReceiver";
	
	@Override
	public void onReceive(Context context, Intent intent) {
		String logPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + context.getPackageName();
		String logFile = logPath + "/log.txt";
		boolean bb = FileUtils.createPath(logPath);
		Log.d(tag, "onReceive() is being called. (b=" + bb + ", logPath=" + logPath + ", logFile=" + logFile + ")");
		
		if (bb == true) {
			TextUtils.writeTextFile(logFile, TextUtils.getMyStandardDateStr(System.currentTimeMillis()) + " onReceive() ..");
		}
		
		if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED) || intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL) ) {
			boolean b = AsyncManager.isServiceRunning(context, com.tnsw.facebookautoliker.services.AutoLikerService.class.getName());
			if (b == false) {
				Intent ii = new Intent(context, AutoLikerService.class);
				context.startService(ii);
				TextUtils.writeTextFile(logFile, TextUtils.getMyStandardDateStr(System.currentTimeMillis()) + " " + AutoLikerService.class.getName() + " is started");
			}
			
		}
	}

}
